package acc_service

import (
	"chat-room/providers"
	"chat-room/websocket/acc_dao"
)

type UserService struct {
}

/**
 * @Author: yang
 * @Description：移除用户
 * @Date: 2021/11/3 14:49
 */
func (us *UserService) RemoveUser(token string) error {
	tx := providers.Mysql
	userDao := &acc_dao.User{
		Token:    token,
		IsDelete: 0,
	}
	user, err := userDao.Find(tx, userDao)
	if err != nil {
	_:
		providers.Logger.Error("移除用户失败：用户不存在", err)
		return nil
	}

	user.IsDelete = 1
	err = user.Save(tx)

	if err != nil {
	_:
		providers.Logger.Error("移除用户失败：用户更新失败", err)
		return nil
	}
	return nil

}

/**
 * @Author: yang
 * @Description：用户Token验证
 * @Date: 2021/11/2 11:39
 */
func (us *UserService) CheckUserToken(token string) error {
	userDao := &acc_dao.User{
		Token:    token,
		IsDelete: 0,
	}
	_, err := userDao.Find(providers.Mysql, userDao)
	return err
}

/**
 * @Author: yang
 * @Description：获取同一房间用户列表(本人除外)
 * @Date: 2021/11/2 16:22
 */
func (us *UserService) GetUserTokenList(token string) (list []string) {

	tx := providers.Mysql
	userDao := &acc_dao.User{
		Token:    token,
		IsDelete: 0,
	}
	user, err := userDao.Find(tx, userDao)
	if err != nil {
		return
	}

	userList, err := userDao.List(tx, &acc_dao.User{
		IsDelete: 0,
		RoomId:   user.RoomId,
	})
	if err != nil {
		return
	}

	for _, val := range userList {
		if token != val.Token {
			list = append(list, val.Token)
		}
	}

	return
}
